package com.csc.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.SessionAttributes;

import com.csc.bean.UserInboxBean;
import com.csc.bean.UserRequestBean;
import com.csc.domain.TransactionHistory;
import com.csc.service.SystemTransactionHistoryService;

@Controller
@SessionAttributes("user")
public class TransactionHistoryController {
	@Autowired
	SystemTransactionHistoryService transHisService;

	static final Logger logger = Logger.getLogger(SystemAdminController.class);

	// Nguyen Vu Hung
	@RequestMapping(value = "/usermessage", method = RequestMethod.GET)
	public String goUserInbox() {
		return "user/message";
	}

	// Nguyen Vu Hung
	@RequestMapping(value = "/usermessageJson", method = RequestMethod.GET)
	public @ResponseBody
	List<UserInboxBean> goUserInboxJSon(@ModelAttribute("user") String username) {
		List<TransactionHistory> listAll = transHisService
				.getAllInboxUser(username);
		return parseToUserInbox(listAll);
	}

	// Nguyen Vu Hung
	@RequestMapping(value = "/searchInboxUserByNameSender/{sendername}", method = RequestMethod.GET)
	public @ResponseBody
	List<UserInboxBean> searchInboxUserByNameSender(
			@PathVariable("sendername") String senderName, Model model,
			@ModelAttribute("user") String userAffected) {
		List<TransactionHistory> listAll = transHisService
				.searchInboxUserBySender(senderName, userAffected);
		return parseToUserInbox(listAll);
	}

	// Nguyen Vu Hung
	@RequestMapping(value = "/searchInboxUserAdvanced/{subject}/{fromdate}/{todate}", method = RequestMethod.GET)
	public @ResponseBody
	List<UserInboxBean> searchInboxUserAdvanced(
			@PathVariable("subject") String subject,
			@PathVariable("fromdate") String fromdate,
			@PathVariable("todate") String todate,
			@ModelAttribute("user") String userAffected) {
		if (subject.equals("null"))
			subject = "";
		if (fromdate.equals("null"))
			fromdate = "";
		if (todate.equals("null"))
			todate = "";

		List<TransactionHistory> listAll = transHisService
				.searchInboxUserAdvanced(userAffected, subject, fromdate,
						todate);

		return parseToUserInbox(listAll);
	}

	// Nguyen Vu Hung
	public List<UserInboxBean> parseToUserInbox(List<TransactionHistory> listAll) {
		List<UserInboxBean> listInbox = new ArrayList<UserInboxBean>();
		String sender = "";
		String subject = "";
		String date = "";
		String message = "";
		for (TransactionHistory inbox : listAll) {
			sender = inbox.getAccImpact().getAccountLoginId();
			subject = inbox.getActionTransaction();
			date = inbox.getDateTransaction();
			message = inbox.getCommand();
			listInbox.add(new UserInboxBean(sender, subject, date, message));
		}
		return listInbox;
	}

	// Nguyen Vu Hung
	@RequestMapping(value = "/listrequestuserjson", method = RequestMethod.GET)
	public @ResponseBody
	List<UserRequestBean> goListRequestUser(
			@ModelAttribute("user") String username) {
		List<TransactionHistory> listAll = transHisService
				.getListRequestUser(username);
		return transHisService.parseUserRequestBean(listAll);
	}
}
